package mudfox.dep;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.concurrent.atomic.AtomicLong;

class Greeting {
    private final long id;
    private final String content;

    Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    long getId() {
        return id;
    }

    String getContent() {
        return content;
    }
}

/*
@RestController : @Controller and @ResponseBody
 */
// @RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    // @GetMapping("/")
    public Greeting index() {
        return new Greeting(counter.incrementAndGet(),
                String.format(template, "World"));
    }

    // @RequestMapping(method=GET)
    // @GetMapping("/greeting")
    public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
        return new Greeting(counter.incrementAndGet(),
                String.format(template, name));
    }

    // @GetMapping("/greeting/{name}")
    public Greeting hello(@PathVariable("name") String name) {
        return new Greeting(counter.incrementAndGet(),
                String.format(template, name));
    }
}